package ClassWork.Work_02;

import java.util.Objects;
import java.util.Scanner;

public class LoginAndRegistration {
    public static void inquire(String[][] arr){
        for (int i = 0; i < arr.length; i++) {
                if (arr[i][0] == null && arr[i][1] == null) {
                    continue;
                }
                System.out.println(arr[i][0] + " " + arr[i][1]);
        }
    }
    public static void registration(String[][] arr){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号：");
        String user = sc.nextLine();
        System.out.println("请输入密码：");
        String pass = sc.nextLine();
        for(int i = 0; i < arr.length; i++){
            if(Objects.equals(arr[i][0], user)){
                System.out.println("该账号已注册！");
                break;
            }else if(arr[i][0] == null){
                arr[i][0] = user;
                arr[i][1] = pass;
                System.out.println("注册成功！");
                break;
            }
        }
    }
    public static boolean login(String[][] arr){
        boolean flag = false;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        for(int i = 0; i < arr.length; i++){
            if(Objects.equals(arr[i][0], username) && Objects.equals(arr[i][1], password)){
                flag = true;
                break;
            }
        }
        return flag;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[][] arr = new String[20][2];
        arr[0][0] = "admin";
        arr[0][1] = "123456";
        while(true){
            System.out.println("请选择功能：" + "1-登录，2-注册，3-查看，4-退出");
            int n = sc.nextInt();
            switch (n){
                case 1:
                    boolean flag = login(arr);
                    if(flag){
                        System.out.println("登录成功！");
                        System.exit(0);
                    }else{
                        System.out.println("登录失败！");
                    }
                    break;
                case 2:
                    registration(arr);
                    break;
                case 3:
                    inquire(arr);
                    break;
                case 4:
                    System.exit(0);
                default:
                    System.out.println("请重新输入！");
            }
        }
    }
}
